from django.db import models

from backend.utils.models import BaseModel
from users.models import User


class Menu(BaseModel):
    TYPE_CHOICE = (
        (0, 'layout'),
        (1, 'menu'),
        (2, 'button'),
    )

    path = models.CharField(max_length=255, verbose_name='菜单路径', null=True, blank=True)
    component = models.CharField(max_length=255, verbose_name='组件', null=True, blank=True)
    redirect = models.CharField(max_length=255, verbose_name='重定向', null=True, blank=True)
    code = models.CharField(max_length=255, verbose_name='菜单权限code', null=True, blank=True)
    meta = models.OneToOneField('Meta', related_name='menu', on_delete=models.CASCADE, db_constraint=False, null=True,
                                blank=True)
    type = models.IntegerField(choices=TYPE_CHOICE, verbose_name='0是目录,1是菜单,2是按钮', default=0)
    parent = models.ForeignKey('self', on_delete=models.DO_NOTHING, related_name='children', null=True, blank=True,
                               db_constraint=False)
    status = models.BooleanField(default=True, verbose_name='状态')

    class Meta:
        db_table = 'menu'
        verbose_name = '菜单'
        verbose_name_plural = verbose_name


class Meta(BaseModel):
    title = models.CharField(max_length=255, verbose_name='路由title  一般必填')
    dynamicLevel = models.IntegerField(verbose_name='动态路由可打开Tab页数', null=True, blank=True)
    realPath = models.CharField(max_length=255, verbose_name='动态路由的实际Path, 即去除路由的动态部分', null=True,
                                blank=True)
    ignoreAuth = models.BooleanField(null=True, blank=True, verbose_name='是否忽略权限，只在权限模式为Role的时候有效')
    roles = models.JSONField(null=True, blank=True, verbose_name='可以访问的角色，只在权限模式为Role的时候有效')
    ignoreKeepAlive = models.BooleanField(null=True, blank=True, verbose_name='是否忽略KeepAlive缓存')
    affix = models.BooleanField(null=True, blank=True, verbose_name='是否固定标签')
    icon = models.CharField(max_length=255, verbose_name='图标，也是菜单图标', null=True, blank=True)
    frameSrc = models.CharField(max_length=255, verbose_name='内嵌iframe的地址', null=True, blank=True)
    transitionName = models.CharField(max_length=255, verbose_name='指定该路由切换的动画名', null=True, blank=True)
    hideBreadcrumb = models.BooleanField(verbose_name='隐藏该路由在面包屑上面的显示', null=True,
                                         blank=True)
    carryParam = models.BooleanField(null=True, blank=True,
                                     verbose_name='如果该路由会携带参数，且需要在tab页上面显示。则需要设置为true')
    hideChildrenInMenu = models.BooleanField(null=True, blank=True, verbose_name='隐藏所有子菜单')
    currentActiveMenu = models.CharField(max_length=255,
                                         verbose_name='当前激活的菜单。用于配置详情页时左侧激活的菜单路径', null=True,
                                         blank=True)
    hideTab = models.BooleanField(null=True, blank=True, verbose_name='当前路由不再标签页显示')
    hideMenu = models.BooleanField(null=True, blank=True, verbose_name='当前路由不再菜单显示')
    orderNo = models.IntegerField(null=True, blank=True, verbose_name='菜单排序，只对第一级有效')
    ignoreRoute = models.BooleanField(null=True, blank=True,
                                      verbose_name='忽略路由。用于在ROUTE_MAPPING以及BACK权限模式下，生成对应的菜单而忽略路由。2.5.3以上版本有效')
    hidePathForChildren = models.BooleanField(null=True, blank=True,
                                              verbose_name='是否在子级菜单的完整path中忽略本级path。2.5.3以上版本有效')

    class Meta:
        db_table = 'meta'
        verbose_name = '菜单meta'
        verbose_name_plural = verbose_name


class Role(BaseModel):
    remark = models.CharField(max_length=255, verbose_name='角色描述', null=True, blank=True)
    status = models.BooleanField(default=1, verbose_name='状态')
    create_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='my_roles', db_constraint=False)
    menus = models.ManyToManyField(Menu, related_name='roles', db_constraint=False)
    user = models.ManyToManyField(User, related_name='roles', db_constraint=False)

    class Meta:
        db_table = 'role'
        verbose_name = '角色'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.name}'
